home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / Terminal 2.2 / Project / Sources / Script.c < prev    next >
Text File  |  1992-01-17  |  13KB  |  545 lines

  1. /*
  2.     Terminal 2.2
  3.     "Script.c"
  4. */
  5.  
  6. #ifdef THINK_C
  7. #include "MacHeaders"
  8. #endif
  9. #ifdef applec
  10. #pragma load ":(Objects):MacHeadersMPW"
  11. #pragma segment Main2
  12. #endif
  13.  
  14. #include "Interp.h"
  15. #include "Text.h"
  16. #include "Main.h"
  17. #include "Document.h"
  18. #include "Procedure.h"
  19. #include "File.h"
  20. #include "FormatStr.h"
  21. #include "Port.h"
  22. #include "Macros.h"
  23. #include "Utilities.h"
  24.  
  25. /* ----- beep() -------------------------------------------------------- */
  26.  
  27. static INTEGER SI_beep(INTEGER params[])
  28. {
  29. #pragma unused(params)
  30.     SysBeep(1);
  31.     return 0;
  32. }
  33.  
  34. /* ----- result = pause(timeout) --------------------------------------- */
  35.  
  36. static INTEGER SI_pause(INTEGER params[])
  37. {
  38.     return (INTEGER)Loop((long)params[0], 0, 0);
  39. }
  40.  
  41. /* ----- result = prompt(string, timeout) ------------------------------ */
  42.  
  43. static INTEGER SI_prompt(INTEGER params[])
  44. {
  45.     register INTEGER result;
  46.     register Byte *s = (Byte *)params[0];
  47.  
  48.     CtoPstr((char *)s);
  49.     result = (INTEGER)Loop((long)params[1], s, 1);
  50.     PtoCstr(s);
  51.     return result;
  52. }
  53.  
  54. /* ----- result = nextline(string, timeout) ---------------------------- */
  55.  
  56. static INTEGER SI_nextline(INTEGER params[])
  57. {
  58.     register INTEGER result;
  59.     register Byte *s = (Byte *)params[0];
  60.  
  61.     result = (INTEGER)Loop((long)params[1], s, 2);
  62.     PtoCstr(s);
  63.     return result;
  64. }
  65.  
  66. /* ----- result = type(template, ...) ---------------------------------- */
  67.  
  68. static INTEGER SI_type(INTEGER params[])
  69. {
  70.     register INTEGER result;
  71.     Byte s[256];
  72.  
  73.     if (params[0]) {
  74.         SFormatStr(s, (Byte *)params[0], ¶ms[1]);
  75.         result = Type(s);
  76.     } else {            /* No formatting */
  77.         CtoPstr((char *)params[1]);
  78.         result = Type((Byte *)params[1]);
  79.         PtoCstr((Byte *)params[1]);
  80.     }
  81.     return result;
  82. }
  83.  
  84. /* ----- result = display(template, ...) ------------------------------- */
  85.  
  86. static INTEGER SI_display(INTEGER params[])
  87. {
  88.     register INTEGER result;
  89.     Byte s[256];
  90.  
  91.     if (params[0]) {
  92.         SFormatStr(s, (Byte *)params[0], ¶ms[1]);
  93.         result = Display(s);
  94.     } else {            /* No formatting */
  95.         CtoPstr((char *)params[1]);
  96.         result = Display((Byte *)params[1]);
  97.         PtoCstr((Byte *)params[1]);
  98.     }
  99.     return result;
  100. }
  101.  
  102. /* ----- format(string, template, ...) --------------------------------- */
  103.  
  104. static INTEGER SI_format(INTEGER params[])
  105. {
  106.     SFormatStr((Byte *)params[0], (Byte *)params[1], ¶ms[2]);
  107.     PtoCstr((Byte *)params[0]);
  108.     return 0;
  109. }
  110.  
  111. /* ----- result = setup(baud,data,parity,stop,port,dtr,handshake) ------ */
  112.  
  113. static INTEGER SI_setup(INTEGER params[])
  114. {
  115.     INTEGER err;
  116.     register INTEGER port = params[4];
  117.     register Boolean newport;
  118.  
  119.     if (newport = (port != -1 && port != 0 && port != 1))
  120.         CtoPstr((char *)port);
  121.     else
  122.         port = 0;
  123.     err = (INTEGER)PortSetUp((short)params[0], (short)params[1],
  124.         (short)params[2], (short)params[3], (Byte *)port,
  125.         (short)params[5], (short)params[6]);
  126.     if (newport)
  127.         PtoCstr((Byte *)port);
  128.     return err;
  129. }
  130.  
  131. /* ----- terminal(lEcho, rEcho, autoLF, save) -------------------------- */
  132.  
  133. static INTEGER SI_terminal(INTEGER params[])
  134. {
  135.     return (INTEGER)TerminalSetup((short)params[0], (short)params[1],
  136.         (short)params[2], (short)params[3]);
  137. }
  138.  
  139. /* ----- lecho(OnOff) -------------------------------------------------- */
  140.  
  141. static INTEGER SI_lecho(INTEGER params[])
  142. {
  143.     return (INTEGER)TerminalSetup((short)params[0], -1, -1, -1);
  144. }
  145.  
  146. /* ----- recho(OnOff) -------------------------------------------------- */
  147.  
  148. static INTEGER SI_recho(INTEGER params[])
  149. {
  150.     return (INTEGER)TerminalSetup(-1, (short)params[0], -1, -1);
  151. }
  152.  
  153. /* ----- autolf(OnOff) ------------------------------------------------- */
  154.  
  155. static INTEGER SI_autolf(INTEGER params[])
  156. {
  157.     return (INTEGER)TerminalSetup(-1, -1, (short)params[0], -1);
  158. }
  159.  
  160. /* ----- save(OnOff) --------------------------------------------------- */
  161.  
  162. static INTEGER SI_save(INTEGER params[])
  163. {
  164.     return (INTEGER)TerminalSetup(-1, -1, -1, (short)params[0]);
  165. }
  166.  
  167. /* ----- result = send(name) ------------------------------------------- */
  168.  
  169. static INTEGER SI_send(INTEGER params[])
  170. {
  171.     register INTEGER result;
  172.     register Byte name[256];
  173.     short volume;
  174.     long directory;
  175.     OSType creator, type;
  176.     long create, modif;
  177.     short ref;
  178.  
  179.     /* Use default folder */
  180.  
  181.     Folder(&volume, &directory);
  182.     CtoPstr((char *)params[0]);
  183.     memcpy(name, (Byte *)params[0], *((Byte *)params[0]) + 1);
  184.     PtoCstr((Byte *)params[0]);
  185.  
  186.     /* See if this is a TEXT file */
  187.  
  188.     if (result = (INTEGER)InfoFile(volume, directory, name,
  189.             &creator, &type, &create, &modif))
  190.         goto done;
  191.     if (type != TEXT) {
  192.         result = 3;
  193.         goto done;
  194.     }
  195.  
  196.     /* Open the file, send it */
  197.  
  198.     if (!(result = (INTEGER)OpenFile(volume, directory, name, &ref)))
  199.         result = (INTEGER)SendTextFile(ref, name);
  200.  
  201. done:
  202.     return result;
  203. }
  204.  
  205. /* ----- result = download(name, binary, zmodem) ----------------------- */
  206.  
  207. static INTEGER SI_download(INTEGER params[])
  208. {
  209.     register INTEGER result;
  210.     register Byte *name = (Byte *)params[0];
  211.  
  212.     if (name)
  213.         CtoPstr((char *)name);
  214.     result = Download(name, params[1], params[2]);
  215.     if (name)
  216.         PtoCstr((Byte *)name);
  217.     return result;
  218. }
  219.  
  220. /* ----- result = upload(name, binary, zmodem) ------------------------- */
  221.  
  222. static INTEGER SI_upload(INTEGER params[])
  223. {
  224.     register INTEGER result;
  225.  
  226.     CtoPstr((char *)params[0]);
  227.     result = Upload((Byte *)params[0], params[1], params[2]);
  228.     PtoCstr((Byte *)params[0]);
  229.     return result;
  230. }
  231.  
  232. /* ----- seconds = time() ---------------------------------------------- */
  233.  
  234. static INTEGER SI_time(INTEGER params[])
  235. {
  236. #pragma unused(params)
  237.     return Time;
  238. }
  239.  
  240. /* ----- date(seconds, &yr, &mo, &da, &ho, &mi, &se, &dw) -------------- */
  241.  
  242. static INTEGER SI_date(INTEGER params[])
  243. {
  244.     DateTimeRec r;
  245.  
  246.     Secs2Date(params[0], &r);
  247.     *(long *)params[1] = r.year;
  248.     *(long *)params[2] = r.month;
  249.     *(long *)params[3] = r.day;
  250.     *(long *)params[4] = r.hour;
  251.     *(long *)params[5] = r.minute;
  252.     *(long *)params[6] = r.second;
  253.     *(long *)params[7] = r.dayOfWeek;
  254.     return 0;
  255. }
  256.  
  257. /* ----- string = new(size) -------------------------------------------- */
  258.  
  259. static INTEGER SI_new(INTEGER params[])
  260. {
  261.     return (INTEGER)NewPtrClear(params[0]);
  262. }
  263.  
  264. /* ----- free(string) -------------------------------------------------- */
  265.  
  266. static INTEGER SI_free(INTEGER params[])
  267. {
  268.     DisposPtr((Ptr)params[0]);
  269.     return 0;
  270. }
  271.  
  272. /* ----- move(source, destination, count) ------------------------------ */
  273.  
  274. static INTEGER SI_move(INTEGER params[])
  275. {
  276.     memmove((void *)params[1], (void *)params[0], params[2]);
  277.     return 0;
  278. }
  279.  
  280. /* ----- result = strcmp(s1, s2) --------------------------------------- */
  281.  
  282. static INTEGER SI_strcmp(INTEGER params[])
  283. {
  284.     register Byte *s1 = (Byte *)params[0];
  285.     register Byte *s2 = (Byte *)params[1];
  286.  
  287.     while (tolower(*s1) == tolower(*s2)) {
  288.         if (!*s1)
  289.             break;
  290.         s1++;
  291.         s2++;
  292.     }
  293.     return (INTEGER)(tolower(*s1) - tolower(*s2));
  294. }
  295.  
  296. /* ----- result = catalog(i,name,&type,&dsize,&rsize,&cdate,&mdate) ---- */
  297.  
  298. static INTEGER SI_catalog(INTEGER params[])
  299. {
  300.     register short err;
  301.     HParamBlockRec p;
  302.     short volume;
  303.     long directory;
  304.  
  305.     if (params[0] == 0)        /* File name is given */
  306.         CtoPstr((char *)params[1]);
  307.     Folder(&volume, &directory);
  308.     memset(&p, 0, sizeof(p));
  309.     p.ioParam.ioVRefNum = volume;
  310.     p.fileParam.ioDirID = directory;
  311.     p.ioParam.ioNamePtr = (StringPtr)params[1];    /* File name */
  312.     p.fileParam.ioFDirIndex = params[0];        /* Index */
  313.     err = PBHGetFInfo(&p, FALSE);
  314.     PtoCstr((Byte *)params[1]);
  315.     if (err)
  316.         return err;
  317.     *((long *)params[2]) = p.fileParam.ioFlFndrInfo.fdType;    /* Type */
  318.     *((long *)params[3]) = p.fileParam.ioFlLgLen;    /* Data fork length */
  319.     *((long *)params[4]) = p.fileParam.ioFlRLgLen;    /* Resource fork */
  320.     *((long *)params[5]) = p.fileParam.ioFlCrDat;    /* Creation date */
  321.     *((long *)params[6]) = p.fileParam.ioFlMdDat;    /* Modification date */
  322.     return 0;
  323. }
  324.  
  325. /* ----- result = getcts() --------------------------------------------- */
  326.  
  327. static INTEGER SI_getcts(INTEGER params[])
  328. {
  329. #pragma unused(params)
  330.     return SerialCTS();
  331. }
  332.  
  333. /* ----- result = getdcd() --------------------------------------------- */
  334.  
  335. static INTEGER SI_getdcd(INTEGER params[])
  336. {
  337. #pragma unused(params)
  338.     return SerialDCD();
  339. }
  340.  
  341. /* ----- setdtr(onoff) ------------------------------------------------- */
  342.  
  343. static INTEGER SI_setdtr(INTEGER params[])
  344. {
  345.     SerialDTR(params[0]);
  346.     return 0;
  347. }
  348.  
  349. /* ----- protocol(crc, k, batch, timeout, binary, b) ------------------- */
  350.  
  351. static INTEGER SI_protocol(INTEGER params[])
  352. {
  353.     return TransferSetup(params[0], params[1], params[2], params[3]);
  354. }
  355.  
  356. /* ----- xyparms(crc, k, batch, timeout) ------------------------------- */
  357.  
  358. static INTEGER SI_xyparms(INTEGER params[])
  359. {
  360.     return XYModemSetup(params[0] != 0, params[1], params[2], params[3]);
  361. }
  362.  
  363. /* ----- zparms(escctl,timeout,retries,buffer,packet,window,crcq) -----  */
  364.  
  365. static INTEGER SI_zparms(INTEGER params[])
  366. {
  367.     return ZModemSetup(params[0] != 0, params[1], params[2], params[3],
  368.         params[4], params[5], params[6]);
  369. }
  370.  
  371. /* ----- err = macrol(name) -------------------------------------------- */
  372.  
  373. static INTEGER SI_macrol(INTEGER params[])    /* Load macro file */
  374. {
  375.     register short err;
  376.  
  377.     CtoPstr((char *)params[0]);
  378.     err = LoadMacros(Settings.scriptVolume, Settings.scriptDirectory,
  379.         (Byte *)params[0]);
  380.     PtoCstr((Byte *)params[0]);
  381.     return err;
  382. }
  383.  
  384. /* ----- err = macrox(i, op, name) ------------------------------------- */
  385.  
  386. static INTEGER SI_macrox(INTEGER params[])    /* Execute macro */
  387. {
  388.     switch (params[1]) {
  389.         case 0:    /* Send */
  390.         case 1:    /* Display */
  391.             break;
  392.         case 2:    /* Get name */
  393.             NameMacro(params[0] + DOMACRO1, (Byte *)params[2]);
  394.             PtoCstr((Byte *)params[2]);
  395.         default:
  396.         return 0;
  397.     }
  398.     return DoMacro(params[0] + DOMACRO1, params[1] != 0);
  399. }
  400.  
  401. /* ----- text(prompt, ldelay, cdelay) ---------------------------------- */
  402.  
  403. static INTEGER SI_text(INTEGER params[])
  404. {
  405.     return TextsendSetup((params[0] < 0) ? 0 : (Byte *)params[0],
  406.         params[1], params[2]);
  407. }
  408.  
  409. /* ----- i = val(str) -------------------------------------------------- */
  410.  
  411. static INTEGER SI_val(INTEGER params[])    /* Convert string to integer */
  412. {
  413.     INTEGER i;
  414.  
  415.     CtoPstr((char *)params[0]);
  416.     StringToNum((Byte *)params[0], &i);
  417.     PtoCstr((Byte *)params[0]);
  418.     return i;
  419. }
  420.  
  421. /* ----- err = capture(onoff, name) ------------------------------------ */
  422.  
  423. static INTEGER SI_capture(INTEGER params[])    /* Capture file on/off */
  424. {
  425.     register DocumentPeek window = TerminalWindow;
  426.     register short err;
  427.     register Byte *name;
  428.     short volume;
  429.     long directory;
  430.     short r;
  431.     OSType creator, type;
  432.     long create, modif;
  433.  
  434.     Folder(&volume, &directory);
  435.     name = (Byte *)params[1];
  436.     switch (params[0]) {
  437.         case 0:                /* Capture off */
  438.             if (!window->file)
  439.                 return 1;    /* Was not open */
  440.             err = (window->length) ?
  441.                 FSWrite(window->file, &window->length, window->record) : 0;
  442.             FSClose(window->file);
  443.             FlushVol(0, window->volume);
  444.             window->file = window->volume = 0;
  445.             window->length = 0;
  446.             SetItemStyle(GetMenu(FILE), CAPTURE, 0);
  447.             return err;
  448.         case 1:                /* Capture on, new file */
  449.             if (window->file)
  450.                 return 2;    /* Still on */
  451.             CtoPstr((char *)name);
  452.             DeleteFile(volume, directory, name);
  453.             if (!(err = CreateFile(volume, directory, name,
  454.                         Settings.textCreator, TEXT)) &&
  455.                     !(err = OpenFile(volume, directory, name, &r))) {
  456.                 window->file = r;
  457.                 window->volume = volume;
  458.                 SetItemStyle(GetMenu(FILE), CAPTURE, ACTIVE);
  459.             }
  460.             PtoCstr((Byte *)name);
  461.             return err;
  462.         case 2:                /* Capture on, append */
  463.             if (window->file)
  464.                 return 2;    /* Still on */
  465.             CtoPstr((char *)name);
  466.             if (err = InfoFile(volume, directory, name,
  467.                     &creator, &type, &create, &modif)) {
  468.                 /* File does not yet exist */
  469.                 if ((err = CreateFile(volume, directory, name,
  470.                             Settings.textCreator, TEXT)) ||
  471.                         (err = OpenFile(volume, directory, name, &r)))
  472.                     goto back;
  473.             } else {
  474.                 /* File already exists */
  475.                 if (type != TEXT) {
  476.                     err = 3;    /* Must be a TEXT file */
  477.                     goto back;
  478.                 }
  479.                 if (err = OpenFile(volume, directory, name, &r))
  480.                     goto back;
  481.                 if (err = SetFPos(r, fsFromLEOF, 0)) {
  482.                     FSClose(r);
  483.                     goto back;
  484.                 }
  485.             }
  486.             window->file = r;
  487.             window->volume = volume;
  488.             SetItemStyle(GetMenu(FILE), CAPTURE, ACTIVE);
  489. back:
  490.             PtoCstr(name);
  491.             return err;
  492.         default:            /* Illegal parameter */
  493.             return 9;
  494.     }
  495. }
  496.  
  497. /* ----- err = nextbuff(char *buff, int count, int timeout) ------------ */
  498.  
  499. static INTEGER SI_nextbuff(INTEGER params[])
  500. {
  501.     return (INTEGER)LoopBuffer((long)params[2],(Byte *)params[0],params[1]);
  502. }
  503.  
  504. /* ----- Script intrinsic functions ------------------------------------ */
  505.  
  506. INTRINSIC Intrinsics[] = {
  507.     (Byte *)"autolf",    SI_autolf,
  508.     (Byte *)"beep",        SI_beep,
  509.     (Byte *)"capture",    SI_capture,
  510.     (Byte *)"catalog",    SI_catalog,
  511.     (Byte *)"date",        SI_date,
  512.     (Byte *)"display",    SI_display,
  513.     (Byte *)"download",    SI_download,
  514.     (Byte *)"format",    SI_format,
  515.     (Byte *)"free",        SI_free,
  516.     (Byte *)"getcts",    SI_getcts,
  517.     (Byte *)"getdcd",    SI_getdcd,
  518.     (Byte *)"lecho",    SI_lecho,
  519.     (Byte *)"macrol",    SI_macrol,
  520.     (Byte *)"macrox",    SI_macrox,
  521.     (Byte *)"move",        SI_move,
  522.     (Byte *)"new",        SI_new,
  523.     (Byte *)"nextbuff",    SI_nextbuff,
  524.     (Byte *)"nextline",    SI_nextline,
  525.     (Byte *)"pause",    SI_pause,
  526.     (Byte *)"prompt",    SI_prompt,
  527.     (Byte *)"protocol",    SI_protocol,
  528.     (Byte *)"recho",    SI_recho,
  529.     (Byte *)"save",        SI_save,
  530.     (Byte *)"send",        SI_send,
  531.     (Byte *)"setdtr",    SI_setdtr,
  532.     (Byte *)"setup",    SI_setup,
  533.     (Byte *)"stack",    SI_stack,        /* Defined in Interp.c */
  534.     (Byte *)"strcmp",    SI_strcmp,
  535.     (Byte *)"terminal",    SI_terminal,
  536.     (Byte *)"text",        SI_text,
  537.     (Byte *)"time",        SI_time,
  538.     (Byte *)"type",        SI_type,
  539.     (Byte *)"upload",    SI_upload,
  540.     (Byte *)"val",        SI_val,
  541.     (Byte *)"xyparms",    SI_xyparms,
  542.     (Byte *)"zparms",    SI_zparms,
  543.     0,            0
  544. };
  545.